using System.Collections.Generic;
using Avalonia.Controls;
using Avalonia.Controls.Primitives;
using Avalonia.Input;
using Avalonia.Interactivity;
using Avalonia.Platform.Storage;
using DynamicData;
using MusicDiary.App.ViewModels;
using MusicDiary.Lib.Models;

namespace MusicDiary.App.Views;

public partial class MainView : UserControl
{
    
    #region Init

    public MainView()
    {
        InitializeComponent();
    }

    #endregion

    #region Menu

    private async void OpenClick(object? sender, RoutedEventArgs e)
    {
        IStorageProvider? sp = GetStorageProvider();
        if (sp == null)
            return;
        var result =
            await sp.OpenFilePickerAsync(new FilePickerOpenOptions() { AllowMultiple = false ,FileTypeFilter = GetFileTypes()});
        if (result.Count == 0) return;
        if (DataContext is MainViewModel data)
        {
            data.PlayChange(result[0].Path.LocalPath);
            data.PlaylistChange();
        }
    }


    private async void AddPlaylistMenuClick(object? sender, RoutedEventArgs e)
    {
        var result =
            await GetStorageProvider()!.OpenFolderPickerAsync(new FolderPickerOpenOptions());
        if (result.Count == 0) return;
        var a = result[0].Path.LocalPath;
        if (DataContext is MainViewModel model)
            model.Add(new PlaylistModel(a));
    }

    #endregion

    #region Player

    private void MusicReleased(object? sender, RangeBaseValueChangedEventArgs e)
    {
        if (DataContext is MainViewModel model)
            model.PosChange((float)(sender as Slider)!.Value);
    }

    private void MusicTapped(object? sender, PointerEventArgs e)
    {
        if (DataContext is MainViewModel model)
            model.Play();
    }

    private void DetailTapped(object? sender, RoutedEventArgs e)
    {
    }

    #endregion

    #region Playlist

    private void SelectionTapped(object? sender, RoutedEventArgs e)
    {
        if ((sender as Control)?.DataContext is MusicModel model)
            (DataContext as MainViewModel)?.PlayChange(model.Url);
    }

    #endregion

    #region Playlists

    private void PlaylistClick(object? sender, RoutedEventArgs e)
    {
        if (sender is not Control control) return;
        if (control.DataContext is not PlaylistModel playlist) return;
        PlaylistsItems.IsVisible = false;
        PlaylistPanel.IsVisible = true;
        PlaylistPanel.DataContext = playlist;
    }

    private void PlaylistSideClick(object? sender, RoutedEventArgs e)
    {
        PlaylistViewer.IsVisible = !PlaylistViewer.IsVisible;
    }

    private void PlaylistsSideClick(object? sender, RoutedEventArgs e)
    {
        PlaylistPanel.IsVisible = false;
        PlaylistPanel.DataContext = "";
        PlaylistsItems.IsVisible = true;
    }

    private void PlaylistChooseClick(object? sender, RoutedEventArgs e)
    {
        if (sender is not Control control) return;
        if (control.DataContext is not MusicModel music) return;
        if (DataContext is not MainViewModel model) return;
        if (PlaylistPanel.DataContext is not PlaylistModel playlist) return;
        model.Playlist.Clear();
        model.Playlist.Add(playlist.Musics);
        model.PlayChange(music.Url);
    }

    #endregion
    
    private IStorageProvider? GetStorageProvider()
    {
        var topLevel = TopLevel.GetTopLevel(this);
        return topLevel?.StorageProvider;
    }
    List<FilePickerFileType> GetFileTypes()
    {
        return new List<FilePickerFileType>
        {
            FilePickerFileTypes.All,
            FilePickerFileTypes.TextPlain
        };
    }

    private void AddMusicOnPlaylistClick(object? sender, RoutedEventArgs e)
    {
        
    }
}